package com.tiger.cloud.controller.crm;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.form.SysDictSmall;
import com.tiger.cloud.model.crm.Peers;
import com.tiger.cloud.service.crm.IPeersService;
import com.tiger.cloud.service.system.CommonDict;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 人脉圈-->同行圈
 */
@Controller
@RequestMapping(value = "peers")
@Api(value = "人脉圈-->同行圈",tags = "同行圈")
public class PeersController extends BaseController {

	@Autowired
	private IPeersService peersService;
	
	/**
	 * 增量获取同行圈
	 */
	@RequestMapping(value = "incrementRecommands", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取同行圈",notes = "增量获取同行圈")
	public Response incrementRecommands(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Peers peers){
		peers.setIsDelete(DeleteEnum.NOMAIL);
		peers.setUserId(userId);
		List<Peers> peersList =peersService.find(peers);
		return ResponseUtil.ok(peersList);
	}
	
	/**
	 * 增增量分页获取同行记录
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量分页获取推荐/申请",notes = "增量分页获取要推荐/申请")
	public Response find(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Peers peers){
		//peers.setIsDelete(DeleteEnum.NOMAIL);
		peers.setUserId(userId);
		Page<Peers> datas=peersService.page(peers);
		
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("user_info");
		Map<String, List<SysDictSmall>> companyValues = CommonDict.getFileds("user_company_info");
		
		values.putAll(companyValues);
		
		datas.setDefaultValues(values);
		return ResponseUtil.ok(datas);
	}
	
	/**
	 * 获取单条推荐/申请
	 */
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取单条推荐/申请",notes = "获取单条要推荐/申请")
	public Response get(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Peers peers){
		peers.setIsDelete(DeleteEnum.NOMAIL);
		peers.setUserId(userId);
		Peers datas=peersService.findOne(peers);
		return ResponseUtil.ok(datas);
	}

	/**
	 * 删除同行
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除同行",notes = "删除同行")
	public Response delete(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Peers peers){
		if(peers.getId()!=null&&peers.getId()>0){
			Peers deleteEntity=peersService.get(peers.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			deleteEntity.setIsDelete(DeleteEnum.DELETE);
			if(peersService.deletePeers(deleteEntity,userId)){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	

}